package game.roam;

import character.CharacterRoamControl;
import character.MyCharacter;
import character.ai.AIGeneralControl;
import com.jme3.app.state.AbstractAppState;
import com.jme3.bullet.collision.shapes.CapsuleCollisionShape;
import game.DataStore;
import game.GameUtil;
import game.InputController;
import player.AIPlayer;

public class RoamAppState extends AbstractAppState {
    RoamInputHandler roamInput = new RoamInputHandler();

    @Override
    public void setEnabled(boolean enabled) {
	super.setEnabled(enabled);
	if(enabled) {
	    for(MyCharacter character : GameUtil.getAllCharactersInWorld()) {
		character.addControl(new CharacterRoamControl(new CapsuleCollisionShape(MyCharacter.RADIUS, MyCharacter.HEIGHT)));
		if(character.getPlayer() instanceof AIPlayer)
                    character.addControl(character.getControl(AIGeneralControl.class).getAILogicRoamControl());
                DataStore.bulletAppState.getPhysicsSpace().addAll(character);
	    }
	    InputController.setCurrentGameInput(roamInput);
	} else {
	    for(MyCharacter character : GameUtil.getAllCharactersInWorld()) {
		DataStore.bulletAppState.getPhysicsSpace().removeAll(character);
		character.removeControl(CharacterRoamControl.class);
                if(character.getPlayer() instanceof AIPlayer)
                    character.removeControl(character.getControl(AIGeneralControl.class).getAILogicRoamControl());
	    }
	}
    }
}
